﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EmoneyWeiXinEntity;
using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using EmoneyWeiXin.Filter;
using System.Data;
using System.Text;
namespace EmoneyWeiXin.Controllers
{
    public class UserGroupManagerController : Controller
    {
        //
        // GET: /UserGroupManager/
        [LoginAuthorizeAttribute]
        public ActionResult Index()
        {
            ViewBag.leftid = int.Parse(Request["leftid"]);
            return View();
        }
        //查询用户组列表
        public string GetUserGroupList()
        {
            var strJson = "";
            var total = 0;
            var pindex = int.Parse(Request["index"]);
            var psize = int.Parse(Request["size"]);
            var weixinid = int.Parse(Request["weixinid"]);
            var list = UserGroupManagerBLL.getUserGroupManagerList(pindex, psize, weixinid, out  total);
            if (list != null)
                strJson = JsonTools.ToJson(list);
            return total + "~" + strJson;
        }
        //根据id查询用户组
        public string GetUserGroupListByID()
        {
            var strJson = "";
            var id = int.Parse(Request["id"]);
            var list = UserGroupManagerBLL.getUserGroupManagerListByID(id);
            if (list != null)
                strJson = JsonTools.ToJson(list);
            return strJson;
        }
        //更新用户组方法
        public string UpdateUserGroup()
        {
            var uid = 0;
            UserEntity us = LoginUserInfo.GetLoginUserCache();
            if (us != null)
                uid = us.id;
            var op = int.Parse(Request["op"]);
            var id = int.Parse(Request["id"]);
            var ire = 0;
            if (op != -1)//编辑操作
            {
                var weixinid = int.Parse(Request["weixinid"]);
                var name = Request["name"];
                var wx_groupid = int.Parse(string.IsNullOrEmpty(Request["wx_groupid"]) == true ? "0" : Request["wx_groupid"]);
                var strRe = "";
                if (id > 0)
                {
                    strRe = UserGroupManagerBLL.EditGroup(weixinid, wx_groupid, name);
                }
                else
                {
                    strRe = UserGroupManagerBLL.CreateGroup(weixinid, name);
                    wx_groupid = int.Parse(strRe == "" ? "0" : strRe);
                }

                var isvalid = 1;
                var obj = new UserGroupManagerEntity();
                obj.id = id;
                obj.name = name;
                obj.isvalid = isvalid;
                obj.createtime = DateTime.Now;
                obj.updatetime = DateTime.Now;
                obj.weixinid = weixinid;
                obj.creator = uid;
                obj.wx_groupid = wx_groupid;
                if (strRe != "")
                    ire = UserGroupManagerBLL.UpdateUserGroupManager(obj);
            }
            else//删除操作
            {
                ire = UserGroupManagerBLL.DeleteUserGroupManager(id);
            }
            return ire.ToString();
        }

        public ActionResult TongbuToServer()
        {
            try
            {
                string id = Request["id"];
                string weixinId = Request["weixinId"];
                string name = Request["name"];
                DataSet ds = UserGroupManagerBLL.GetOpenidListByGroupId(weixinId, id); //获取到该组内的所有用户
                DataTable dt = ds.Tables[0]; //获取openid集合
                DataTable dt2 = ds.Tables[1];//根据groupid，在微信后台的groupid
                StringBuilder sb = new StringBuilder();
                int wx_groupId = Convert.ToInt32(dt2.Rows[0][0]);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sb.Append("\"" + dt.Rows[i][0] + "\",");
                    if (i % 50 == 49 || i == dt.Rows.Count - 1)
                    {
                        string reslt = UserGroupManagerBLL.BatchMoveUserGroup(int.Parse(weixinId), sb.ToString().TrimEnd(','), wx_groupId);
                        if (reslt != "ok")
                        {
                            return Content("0");
                        }
                        sb.Clear();
                    }
                }
                return Content("1");
            }
            catch (Exception)
            {
                return Content("0");
            }


        }

    }
}
